/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.dynread.com
 * 注意：
 * 本软件为www.dynread.com开发研制，项目使用请保留此说明
 */
package com.dynread.mall.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dynread.mall.dto.CateCountDTO;
import com.dynread.mall.dto.ChartDataDTO;
import com.dynread.mall.entity.OrderInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.io.Serializable;
import java.util.List;

/**
 * 商城订单
 *
 * @author drinvay
 * @date 2022/5/7 11:17
 */
public interface OrderInfoMapper extends BaseMapper<OrderInfo> {

	IPage<OrderInfo> selectPage1(IPage<OrderInfo> page, @Param("query") OrderInfo orderInfo);

	IPage<OrderInfo> selectPage2(IPage<OrderInfo> page, @Param("query") OrderInfo orderInfo);

	OrderInfo selectById2(Serializable id);

	/**
	 * 按大于等于的支付时间计数
	 *
	 * @param time
	 * @return java.lang.Integer
	 * @author drinvay
	 * @date 2022/5/7 11:20
	 */
	@Select("SELECT COUNT(id) FROM order_info WHERE payment_time >= #{time}")
	Integer countByPayTimeGreaterThanEqual(@Param("time") String time);

	/**
	 * 按支付时间小于等和支付时间大于等于计数
	 *
	 * @param endTime
	 * @param startTime
	 * @return java.lang.Integer
	 * @author drinvay
	 * @date 2022/5/7 11:52
	 */
	@Select("SELECT COUNT(id) FROM order_info WHERE payment_time < #{endTime} AND payment_time >= #{startTime}")
	Integer countByPayTimeLessThanAndPayTimeGreaterThanEqual(@Param("endTime") String endTime, @Param("startTime") String startTime);

	/**
	 * 订单金额汇总
	 *
	 * @return java.lang.Double
	 * @author drinvay
	 * @date 2022/5/7 11:49
	 */
	@Select("SELECT IFNULL(SUM(payment_price), 0) FROM order_info WHERE status = 3 AND del_flag = 0")
	Double sumTotalPrice();

	/**
	 * 获取订单图形列表数据
	 *
	 * @param time
	 * @return java.util.List<com.dynread.mall.dto.ChartDataDTO>
	 * @author drinvay
	 * @date 2022/5/7 14:31
	 */
	@Select("SELECT IFNULL(sum(payment_price), 0) AS num, " +
			"DATE_FORMAT(create_time, '%m-%d') AS time " +
			"FROM order_info WHERE status = 3 AND del_flag = 0 AND payment_time >= #{time} " +
			"GROUP BY DATE_FORMAT(create_time, '%Y-%m-%d') " +
			"ORDER BY create_time ASC")
	List<ChartDataDTO> chartList(@Param("time") String time);

	@Select("SELECT COUNT(id) AS num, " +
			"DATE_FORMAT(create_time, '%m-%d') AS time " +
			"FROM order_info WHERE status = 3 AND del_flag = 0 AND payment_time >= #{time} " +
			"GROUP BY DATE_FORMAT(create_time, '%Y-%m-%d') " +
			"ORDER BY create_time ASC")
	List<ChartDataDTO> chartListT(@Param("time") String time);

	/**
	 * 获取已付款订单商品二级或一级分类名称
	 *
	 * @return java.util.List<com.dynread.mall.dto.CateCountDTO>
	 * @author drinvay
	 * @date 2022/5/7 15:58
	 */
	@Select("SELECT IFNULL(d.name, e.name) AS catename FROM order_item a " +
			"LEFT JOIN order_info b ON a.order_id = b.id " +
			"LEFT JOIN goods_spu c ON a.spu_id = c.id " +
			"LEFT JOIN goods_category d ON c.category_second = d.id " +
			"LEFT JOIN goods_category e ON c.category_first = e.id " +
			"WHERE b.status = 3 AND b.del_flag = 0")
	List<CateCountDTO> getCateName();
}
